package com.app.TCES.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.app.TCES.domain.Class;
import com.app.TCES.domain.Department;
import com.app.TCES.domain.Student_Dept_Class;
import com.app.TCES.service.A_studentService;


@Controller
public class A_studentController {

	Logger log = LogManager.getLogger(A_studentController.class);

	@Autowired
	A_studentService a_studentService;

	// 查询学生信息
	@ResponseBody
	@RequestMapping(value = "/selectStudent")
	public Map<String, Object> toSelectStudent(@RequestParam("page")Integer page,@RequestParam("limit")Integer limit) {
		
		int tmp = (page-1)*limit;
		int tol = a_studentService.total();
		
		
		log.debug("查询学生信息");
		List<Student_Dept_Class> list = new ArrayList<Student_Dept_Class>();
		Map<String, Object> map = new HashMap<String, Object>();
		list = a_studentService.selectStudents(tmp,limit);
		log.debug("返回学生信息");
		map.put("tol", tol);
		map.put("message", "学生信息查询成功");
		map.put("status", 200);
		map.put("list", list);

		return map;
	}
	
	
	//查询院系
	@ResponseBody
	@RequestMapping(value = "/selectDept",method = RequestMethod.POST)
	public List<Department> showDept(){
		log.debug("查询院系信息");
		List<Department> list = new ArrayList<Department>();
		list = a_studentService.selecDepartments();
		log.debug("返回院系信息");
		return list;
	}
	
	//通过院系(Name)查班级
	@ResponseBody
	@RequestMapping(value = "/selectClass",method = RequestMethod.POST)
	public List<Class> showClasses(String deptName){
		
		log.debug(deptName);
		log.debug("查询院系下的班级");
		List<Class> list = a_studentService.selcetClasses(deptName);
		log.debug("返回院系下的班级信息");
		
		return list;
	}
	
	//通过院系(Id)查班级
	@ResponseBody
	@RequestMapping(value = "/selectC",method = RequestMethod.POST)
	public List<Class> showC(int deptId){
		log.debug("A_studentController"+deptId);
		List<Class> list = a_studentService.selcetC(deptId);
		return list;
	}
	
	//点击学院按钮查学生
	//当点击全部班级时
	@ResponseBody
	@RequestMapping(value = "/selectDept_student")
	public Map<String , Object> selectDept_Student(@RequestParam("page")Integer page,@RequestParam("limit")Integer limit,String deptName){
		int tmp = (page-1)*limit;
		int tol = a_studentService.total1(deptName);
		
		log.debug("=========="+deptName);
		Map<String, Object> map = new HashMap<String, Object>();
		List<Student_Dept_Class> list = new ArrayList<Student_Dept_Class>();
		list = a_studentService.selectDept_Student(tmp,limit,deptName);
		
		map.put("tol", tol);
		map.put("message", "学生信息查询成功");
		map.put("status", 200);
		map.put("list", list);
		
		return map;
	}
	
	
	
	
	//点击班级按钮查询学生
	@ResponseBody
	@RequestMapping(value = "/selectCalss_student")
	public Map<String , Object> selectCalss_Student(@RequestParam("page")Integer page,@RequestParam("limit")Integer limit,String className){
		int tmp = (page-1)*limit;
		int tol = a_studentService.total2(className);
		log.debug("==========="+className);
		Map<String, Object> map = new HashMap<String, Object>();
		List<Student_Dept_Class> list = new ArrayList<Student_Dept_Class>();
		list = a_studentService.selectClass_Studnet(tmp,limit,className);
		
		map.put("tol", tol);
		map.put("message", "学生信息查询成功");
		map.put("status", 200);
		map.put("list", list);
		
		return map;
	}
	
	//更新学生信息
	@ResponseBody
	@RequestMapping(value = "/updateStudent")
	public Map<String , String> updateStudent(int stuId,int deptId,int classId){
		log.debug("开始修改信息");
		a_studentService.updateStudent(stuId, deptId, classId);
		log.debug("修改信息完毕");
		Map<String, String> map = new HashMap<String, String>();
		map.put("msg", "YES");
		return map;
	}
	
	//刪除
	@ResponseBody
	@RequestMapping(value = "/del",method = RequestMethod.POST)
	public Map<String , String> deleteData(int stuId){
		log.debug("======");
		a_studentService.delStudent(stuId);
		Map<String, String> map = new HashMap<String, String>();
		map.put("msg", "YES");
		return map;
		
		
	}
	
	//添加学生
	@ResponseBody
	@RequestMapping(value = "/insertStudent",method = RequestMethod.POST)
	public Map<String, String>  insertStu(int stuId,String stuName,int deptId,int classId){
		log.debug("执行添加");
		String a = a_studentService.insertStudent(stuId, stuName, deptId, classId);
		Map<String , String> map = new HashMap<String, String>();
		map.put("msg", a);
		return map;
		
	}
	
	//模糊查询
	@ResponseBody
	@RequestMapping(value = "/FuzzyQuery")
	public Map<String,Object> FQ(@RequestParam("page")Integer page,@RequestParam("limit")Integer limit,String msg,String deptName,String className){
		int tmp = (page-1)*limit;
		int tol = a_studentService.total3(msg,deptName,className);
		
		Map<String, Object> map = new HashMap<String, Object>();
		List<Student_Dept_Class> list = a_studentService.FQ(tmp,limit,msg,deptName,className);
		map.put("list", list);
		map.put("msg", "success");
		map.put("status", 200);
		map.put("tol", tol);
		return map;
	}
	
	//重置密码
	@ResponseBody
	@RequestMapping("/RP")
	public Map<String, String> RP(int stuId) {
		String a =a_studentService.updatePassword(stuId);
		Map<String, String> map = new HashMap<String, String>();
		map.put("msg", a);
		return map;
	}
	
}
